import { message } from 'antd';
import {
  queryPorduct,
  queryModel,
  queryDecoder,
  queryDetail,
  add,
  del,
  edit,
  publish,
  addTopic,
  queryTopics,
  delTopic,
  qryStandardFunction,
  qryCustomFunction,
  addProp,
  queryProp,
  editProp,
  delProp,
  qryCustomProp,
  qryStandardProp,
  qryProtocolInfo,
  queryEncoder,
  editProtocol,
  qryTcpMainProtocol,
  qryTcpChildProtocol,
  qryRuleType,
  qryProductProps,
  addTcpProtocol,
  delTcpProtocol,
  qryTcpDetailInfo,
  editTcpProtocol,
  clearTcpRule,
  qryMqttProtocol,
  addOrUpdateMqttProtocol,
  qryProductService,
  qryProtocolField,
  addService,
  qryServiceById,
  editService,
  delService,
} from '@/services/product';
import { queryModelClassTree } from '@/services/modelclass';
import { qryUnitList, qryDataTypeList } from '@/services/productmodel';

export default {
  namespace: 'product',

  state: {
    list: [],
    pagination: { pageSize: 10 },
    current: {},
    classes: [],
    decoder: [],
    encoder: [],
    detail: {},
    topics: [],
    models: [],
    standardFunctionList: [],
    customFunctionList: [],
    dataTypeList: [],
    unitList: [],
    functionDetail: {},
    standardPropList: [],
    customPropList: [],
    protocolInfo: {},
    mainSegmentRules: [],
    ruleTypeList: [],
    tcpMainProtocolList: [],
    tcpChildProtocolList: [],
    productPropList: [],
    checkRuleList: [],
    protocolDetail: {},
    mqttEncoderDetail: {},
    mqttDecoderDetail: {},
    services: [],
    fieldList: [],
    serviceParamDetail: {},
    serviceDetail: {},
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryPorduct, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'save',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchClass({ payload }, { call, put }) {
      const response = yield call(queryModelClassTree, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveClass',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchModel({ payload }, { call, put }) {
      const response = yield call(queryModel, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveModel',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },

    *fetchDecoder({ payload }, { call, put }) {
      const response = yield call(queryDecoder, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDecoder',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchEncoder({ payload }, { call, put }) {
      const response = yield call(queryEncoder, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveEncoder',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(queryDetail, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDetail',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *add({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(add, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({ type: 'fetch' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *del({ payload }, { call, put }) {
      const response = yield call(del, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({ type: 'fetch' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *edit({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(edit, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'fetchDetail',
          payload: payload.id,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *publish({ payload }, { call, put }) {
      const response = yield call(publish, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'fetchDetail',
          payload: payload.id,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *addTopic({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(addTopic, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'topics',
          payload: payload.productId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *topics({ payload }, { call, put }) {
      const response = yield call(queryTopics, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveTopics',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *delTopic({ payload }, { call, put }) {
      const response = yield call(delTopic, payload.id);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'topics',
          payload: payload.productId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },

    *fetchStandardFunction({ payload }, { call, put }) {
      const response = yield call(qryStandardFunction, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveStandardFunction',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchStandardProp({ payload }, { call, put }) {
      const response = yield call(qryStandardProp, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveStandardProp',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchCustomProp({ payload }, { call, put }) {
      const response = yield call(qryCustomProp, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveCustomProp',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },

    *fetchCustomFunction({ payload }, { call, put }) {
      const response = yield call(qryCustomFunction, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveCustomFunction',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchUnit({ payload }, { call, put }) {
      const response = yield call(qryUnitList, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveUnit',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDataType({ payload }, { call, put }) {
      const response = yield call(qryDataTypeList, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDataType',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *addProp({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(addProp, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'fetchCustomProp',
          payload: { productId: payload.productId },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchProp({ payload, handleEditModalVisible }, { call, put }) {
      const response = yield call(queryProp, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveFunctionDetail',
          payload: response,
        });
        handleEditModalVisible(true);
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *editProp({ payload, form, handleEditModalVisible }, { call, put }) {
      const response = yield call(editProp, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleEditModalVisible();
        yield put({
          type: 'fetchCustomProp',
          payload: { productId: payload.productId },
        });
        yield put({
          type: 'fetchStandardProp',
          payload: payload.productId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *delProp({ payload, params }, { call, put }) {
      const response = yield call(delProp, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'fetchCustomProp',
          payload: { productId: params.id },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchProtocol({ payload }, { call, put }) {
      const response = yield call(qryProtocolInfo, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveProtocolInfo',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *editProtocol({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(editProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'fetchProtocol',
          payload: payload.productId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchTcpMainProtocol({ payload }, { call, put }) {
      const response = yield call(qryTcpMainProtocol, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveTcpMainProtocol',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchTcpChildProtocol({ payload }, { call, put }) {
      const response = yield call(qryTcpChildProtocol, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveTcpChildProtocol',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchRuleTypes({ payload }, { call, put }) {
      const response = yield call(qryRuleType, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveRuleType',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchProductProps({ payload }, { call, put }) {
      const response = yield call(qryProductProps, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveProductProp',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *addTcpProtocol({ payload, form, isAddMainSegment, handleModalVisible }, { call, put }) {
      const response = yield call(addTcpProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        if (isAddMainSegment) {
          yield put({
            type: 'fetchTcpMainProtocol',
            payload: payload.protocolId,
          });
        } else {
          yield put({
            type: 'fetchTcpChildProtocol',
            payload: payload.protocolId,
          });
        }
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *delTcpChildProtocol({ payload, protocolId }, { call, put }) {
      const response = yield call(delTcpProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'fetchTcpChildProtocol',
          payload: protocolId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *delTcpMainProtocol({ payload, protocolId }, { call, put }) {
      const response = yield call(delTcpProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'fetchTcpMainProtocol',
          payload: protocolId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchTcpInfo({ payload, handleEditModalVisible }, { call, put }) {
      const response = yield call(qryTcpDetailInfo, payload);
      if (response.httpCode === 200) {
        handleEditModalVisible(true);
        yield put({
          type: 'saveProtocolDetail',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *editTcpProtocol({ payload, form, isEditMainSegment, handleEditModalVisible }, { call, put }) {
      const response = yield call(editTcpProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleEditModalVisible();
        if (isEditMainSegment) {
          yield put({
            type: 'fetchTcpMainProtocol',
            payload: payload.protocolId,
          });
        } else {
          yield put({
            type: 'fetchTcpChildProtocol',
            payload: payload.protocolId,
          });
        }
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *clearTcpRule({ payload }, { call, put }) {
      const response = yield call(clearTcpRule, payload.id);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'fetchTcpMainProtocol',
          payload: payload.id,
        });
        yield put({
          type: 'fetchTcpChildProtocol',
          payload: payload.id,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchMqttEncoderProtocol({ payload }, { call, put }) {
      const response = yield call(qryMqttProtocol, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveMqttEncoderProtocol',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchMqttDecoderProtocol({ payload }, { call, put }) {
      const response = yield call(qryMqttProtocol, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveMqttDecoderProtocol',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *addOrUpdateMqttProtocol({ payload }, { call }) {
      const response = yield call(addOrUpdateMqttProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },

    *fetchProductService({ payload }, { call, put }) {
      const response = yield call(qryProductService, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveProductService',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchProtocolField({ payload }, { call, put }) {
      const response = yield call(qryProtocolField, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveProtocolField',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *addService({ payload, form, handleModalVisible, clearParamList }, { call, put }) {
      const response = yield call(addService, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        clearParamList();
        yield put({
          type: 'fetchProductService',
          payload: { productId: payload.productId },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchServiceParam({ payload }, { put }) {
      yield put({
        type: 'saveServiceParam',
        payload: payload.paramItem,
      });
    },
    *fetchServiceById({ payload, handleEditModalVisible, formRef }, { call, put }) {
      const response = yield call(qryServiceById, payload);
      if (response.httpCode === 200) {
        handleEditModalVisible(true);
        formRef.changeParamList(response.data.serviceParamList);
        yield put({
          type: 'saveProtocolService',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *editService({ payload, form, handleEditModalVisible, clearParamList }, { call, put }) {
      const response = yield call(editService, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleEditModalVisible();
        clearParamList();
        yield put({
          type: 'fetchProductService',
          payload: { productId: payload.productId },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *delService({ payload, params }, { call, put }) {
      const response = yield call(delService, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'fetchProductService',
          payload: { productId: params.id },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload.data,
        pagination: {
          current: action.payload.current,
          pageSize: action.payload.size,
          total: action.payload.iTotalRecords,
        },
      };
    },
    saveClass(state, action) {
      return {
        ...state,
        classes: action.payload.data,
      };
    },
    saveModel(state, action) {
      return {
        ...state,
        models: action.payload.data,
      };
    },
    saveDecoder(state, action) {
      return {
        ...state,
        decoder: action.payload.data,
      };
    },
    saveEncoder(state, action) {
      return {
        ...state,
        encoder: action.payload.data,
      };
    },
    saveDetail(state, action) {
      return {
        ...state,
        detail: action.payload.data,
      };
    },
    saveTopics(state, action) {
      return {
        ...state,
        topics: action.payload.data,
      };
    },

    saveStandardFunction(state, action) {
      return {
        ...state,
        standardFunctionList: action.payload.data,
      };
    },

    saveCustomFunction(state, action) {
      return {
        ...state,
        customFunctionList: action.payload.data,
      };
    },
    saveStandardProp(state, action) {
      return {
        ...state,
        standardPropList: action.payload.data,
      };
    },
    saveCustomProp(state, action) {
      return {
        ...state,
        customPropList: action.payload.data,
        pagination: {
          current: action.payload.current,
          pageSize: action.payload.size,
          total: action.payload.iTotalRecords,
        },
      };
    },
    saveUnit(state, action) {
      return {
        ...state,
        unitList: action.payload.data,
      };
    },
    saveDataType(state, action) {
      return {
        ...state,
        dataTypeList: action.payload.data,
      };
    },
    saveFunctionDetail(state, action) {
      return {
        ...state,
        functionDetail: action.payload.data,
      };
    },
    saveProtocolInfo(state, action) {
      return {
        ...state,
        protocolInfo: action.payload.data,
      };
    },
    saveTcpMainProtocol(state, action) {
      return {
        ...state,
        tcpMainProtocolList: action.payload.data,
      };
    },
    saveTcpChildProtocol(state, action) {
      return {
        ...state,
        tcpChildProtocolList: action.payload.data,
      };
    },
    saveRuleType(state, action) {
      return {
        ...state,
        ruleTypeList: action.payload.data,
      };
    },
    saveProductProp(state, action) {
      return {
        ...state,
        productPropList: action.payload.data,
      };
    },
    saveProtocolDetail(state, action) {
      return {
        ...state,
        protocolDetail: action.payload.data,
      };
    },
    saveMqttEncoderProtocol(state, action) {
      return {
        ...state,
        mqttEncoderDetail: action.payload.data,
      };
    },
    saveMqttDecoderProtocol(state, action) {
      return {
        ...state,
        mqttDecoderDetail: action.payload.data,
      };
    },
    saveProductService(state, action) {
      return {
        ...state,
        services: action.payload.data,
        pagination: {
          current: action.payload.current,
          pageSize: action.payload.size,
          total: action.payload.iTotalRecords,
        },
      };
    },
    saveProtocolField(state, action) {
      return {
        ...state,
        fieldList: action.payload.data,
      };
    },
    saveServiceParam(state, action) {
      return {
        ...state,
        serviceParamDetail: action.payload,
      };
    },
    saveProtocolService(state, action) {
      return {
        ...state,
        serviceDetail: action.payload.data,
      };
    },
  },
};
